﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LotteryVote.Entity;
using System.Data.SqlClient;

namespace LotteryVote.Data
{
    public class UserCommissionDataAccess : DataBase
    {
        public void InsertUserCommission(UserCommission userComms)
        {
            string sql = string.Format(@"INSERT INTO {0} ({1},{2}) VALUES (@{1},@{2})  SELECT IDENT_CURRENT('{0}')",
                UserCommission.TABLENAME, UserCommission.USERID, UserCommission.SPECIEID);
            object id = base.ExecuteScalar(sql, new SqlParameter(UserCommission.USERID, userComms.UserId),
                new SqlParameter(UserCommission.SPECIEID, userComms.SpecieId));
            userComms.CommissionId = Convert.ToInt32(id);
        }

        public UserCommission GetUserCommission(int commissionId)
        {
            string sql = string.Format(@"SELECT * FROM {0} WHERE {1}=@{1}", UserCommission.TABLENAME, UserCommission.COMMISSIONID);
            return base.ExecuteEntity<UserCommission>(sql, new SqlParameter(UserCommission.COMMISSIONID, commissionId));
        }
        public UserCommission GetUserCommission(User user, LotterySpeciesEnum specie)
        {
            string sql = string.Format(@"SELECT * FROM {0} WHERE {1}=@{1} AND {2}=@{2}", UserCommission.TABLENAME, UserCommission.USERID,
                UserCommission.SPECIEID);
            return base.ExecuteEntity<UserCommission>(sql, new SqlParameter(UserCommission.USERID, user.UserId),
                new SqlParameter(UserCommission.SPECIEID, (int)specie));
        }
        public IList<UserCommission> GetUserCommission(User user)
        {
            string sql = string.Format(@"SELECT * FROM {0} WHERE {1}=@{1}", UserCommission.TABLENAME, UserCommission.USERID);
            return base.ExecuteList<UserCommission>(sql, new SqlParameter(UserCommission.USERID, user.UserId));
        }

    }
}
